//
//  DVShareCustom.m
//  DoctorVideo
//
//  Created by edz on 2020/5/26.
//  Copyright © 2020 lzx. All rights reserved.
//

#import "DVShareCustom.h"
//#import <ShareSDK/ShareSDK.h>
//#import <ShareSDKUI/ShareSDK+SSUI.h>
//屏幕宽度相对iPhone6屏幕宽度的比例
#define KWidth_Scale    [UIScreen mainScreen].bounds.size.width/375.0f

@interface DVShareCustom ()



@end

@implementation DVShareCustom

static id _publishContent;

+ (void)shareWithContent:(id)publishContent
{
    _publishContent = publishContent;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    blackView.backgroundColor = JSS_RGBA_COLOR(0, 0, 0, 0.45);
    blackView.tag = 440;
    [window addSubview:blackView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissShareCustom)];
    [blackView addGestureRecognizer:tap];
    
    YCShadowView *shareView = [[YCShadowView alloc] init];
    shareView.frame = CGRectMake(0, kScreenHeight - 264, kDeviceWidth, 264);
    shareView.backgroundColor = RGB_VALUE(0xFFFFFF);
    shareView.tag = 441;
    [shareView yc_cornerRadius:20 byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight];
    [window addSubview:shareView];
//    [shareView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.offset(0);
//        make.height.mas_equalTo(264);
//        make.bottom.offset(0);
//    }];
    
//    UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(0, (kScreenHeight-200*KWidth_Scale), kScreenWidth, 200*KWidth_Scale)];
//    shareView.backgroundColor = JSS_RGBA_COLOR(249, 249, 249, 1);
//    shareView.tag = 441;
//    [window addSubview:shareView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, shareView.width, 45*KWidth_Scale)];
    titleLabel.text = @"分享到";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:17*KWidth_Scale];
    [shareView addSubview:titleLabel];
    
    NSArray *btnImages = @[@"img_pengyouquan", @"img_weixin", @"img_weibo"];
    NSArray *btnTitles = @[ @"朋友圈", @"微信好友", @"微博"];
    for (NSInteger i=0; i<3; i++) {
        CGFloat top = 0.0f;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(40*KWidth_Scale+i*90*KWidth_Scale+(kScreenWidth-(270+80)*KWidth_Scale)/2*i, titleLabel.bottom+top, 90*KWidth_Scale, 90*KWidth_Scale)];
        [button setImage:[UIImage imageNamed:btnImages[i]] forState:UIControlStateNormal];
        [button setTitle:btnTitles[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:10*KWidth_Scale];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitleColor:RGB_VALUE(0x000000) forState:UIControlStateNormal];
        [button layoutButtonWithEdgeInsetsStyle:DVButtonEdgeInsetsStyleTop imageTitleSpace:15];
        button.tag = 331+i;
        [button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [shareView addSubview:button];
    }
    
    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, 173, kScreenWidth-32, 51*KWidth_Scale)];
    [cancleBtn setBackgroundColor:RGB_VALUE(0xDC0000)];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    cancleBtn.layer.masksToBounds = YES;
    cancleBtn.layer.cornerRadius = 12;
    cancleBtn.tag = 339;
    [cancleBtn addTarget:self action:@selector(dismissShareCustom) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:cancleBtn];
    
    //为了弹窗不那么生硬，这里加了个简单的动画
//    shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
//    blackView.alpha = 0;
//    [UIView animateWithDuration:0.3f animations:^{
//        shareView.transform = CGAffineTransformMakeScale(1, 1);
//        blackView.alpha = 1;
//    } completion:^(BOOL finished) {
//
//    }];
}
     
+ (void)dismissShareCustom
{
    //    NSLog(@"%@",[ShareSDK version]);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *blackView = [window viewWithTag:440];
    UIView *shareView = [window viewWithTag:441];
    
    //为了弹窗不那么生硬，这里加了个简单的动画
//    shareView.transform = CGAffineTransformMakeScale(1, 1);
//    [UIView animateWithDuration:0.3f animations:^{
//        shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
        blackView.alpha = 0;
//    } completion:^(BOOL finished) {
        
        [shareView removeFromSuperview];
        [blackView removeFromSuperview];
//    }];
}

+ (void)shareBtnClick:(UIButton *)btn
{
    [self dismissShareCustom];
    int shareType = 0;
    id publishContent = _publishContent;
//    switch (btn.tag) {
//        case 331:
//        {
//            shareType = SSDKPlatformSubTypeWechatTimeline;
//        }
//            break;
//
//        case 332:
//        {
//            shareType = SSDKPlatformSubTypeWechatSession;
//        }
//            break;
//
//        case 333:
//        {
//            shareType = SSDKPlatformTypeSinaWeibo;
//        }
//            break;
//        default:
//            break;
//    }
//    [ShareSDK share:shareType parameters:publishContent onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
//        switch (state) {
//            case SSDKResponseStateSuccess:
//               {
//                    NSLog(@"成功");//成功
//                    break;
//               }
//            case SSDKResponseStateFail:
//               {
//                    NSLog(@"--%@",error.description);//失败
//                    break;
//               }
//            case SSDKResponseStateCancel:
//               {
//                    NSLog(@"--%@",error.description);//取消
//                    break;
//               }
//            break;
//            default:
//            break;
//        }
//    }];
}

@end
